package com.verifone.tony.databasetest;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;

import com.raizlabs.android.dbflow.config.FlowConfig;
import com.raizlabs.android.dbflow.config.FlowManager;
import com.raizlabs.android.dbflow.sql.language.Select;
import com.raizlabs.android.dbflow.structure.ModelAdapter;

import static com.verifone.tony.databasetest.MyDatabase.VERSION;


public class MainActivity extends AppCompatActivity {
    private static final String TAG = "------------Database";
    private MyDatabaseHelper dbHelper;
    Context context = this;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        //创建数据库
        Button createDatabase = (Button) findViewById(R.id.create_database);
        createDatabase.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                //--------------------- sqlite --------------------------
                //用dbHelper前必须执行这个
                dbHelper = new MyDatabaseHelper(context, "BookStore.db", null, 2);//2是version
                dbHelper.getReadableDatabase();

                //--------------------- dbflow --------------------------
                //用dbFlowModel前必须执行这个
                FlowManager.init(new FlowConfig.Builder(context).build());
            }
        });

        //向数据库添加表
        Button addData = (Button) findViewById(R.id.add_data);
        addData.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                //--------------------- sqlite --------------------------
                SQLiteDatabase db = dbHelper.getWritableDatabase();
                ContentValues values = new ContentValues();
                values.put("name", "The Da Vinci Code");
                values.put("author", "Dan Brown");
                values.put("pages", "454");
                values.put("price", "16.96");
                db.insert("Book", null, values);
                values.clear();

                values.put("name", "The Lost Symbol");
                values.put("author", "Dan Brown");
                values.put("pages", "516");
                values.put("price", "19.95");
                db.insert("Book", null, values);

                //--------------------- dbflow --------------------------
                //表名DBFlowModel
                DBFlowModel dbFlowModel = new DBFlowModel();
                dbFlowModel.id = 1;
                dbFlowModel.setModelname("dbtest1");
                dbFlowModel.setName("Ruomiz");
                dbFlowModel.setAddress("Wuhan");
                dbFlowModel.setAge(100);
                dbFlowModel.save();
            }
        });

        //改表里的数据
        Button updateData = (Button) findViewById(R.id.update_data);
        updateData.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                //--------------------- sqlite --------------------------
                SQLiteDatabase db = dbHelper.getWritableDatabase();
                ContentValues values = new ContentValues();
                values.put("price", "10.99");
                //values.put("alias", "tony");
                db.update("Book", values, "name = ?", new String[] { "The Da Vinci Code" });

                ModelAdapter<DBFlowModel> adapter = FlowManager.getModelAdapter(DBFlowModel.class);
                DBFlowModel dbFlowModel_new = new DBFlowModel();
                ////这一行必须加，必须指定id，后面才能根据id修改某一条的数据，否则update不起作用
                dbFlowModel_new.id = 1;
                dbFlowModel_new.setModelname("dbtest1");
                dbFlowModel_new.setName("ZhouChong");
                dbFlowModel_new.setAddress("BeiJing");
                dbFlowModel_new.setAge(150);
                if (VERSION != 1) {
                    dbFlowModel_new.setPhone("13333333333");
                    dbFlowModel_new.setEmail("chong.z@verifone.cn");
                }
                adapter.update(dbFlowModel_new);
            }
        });

        //删除表里的数据
        Button deleteData = (Button) findViewById(R.id.delete_data);
        deleteData.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                //--------------------- sqlite --------------------------
                SQLiteDatabase db = dbHelper.getWritableDatabase();
                db.delete("Book", "pages > ?", new String[] { "500" });
            }
        });

        //查询表里的数据
        Button queryData = (Button) findViewById(R.id.query_data);
        queryData.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                //--------------------- sqlite --------------------------
                SQLiteDatabase db = dbHelper.getWritableDatabase();
                Cursor cursor = db.query("Book", null, null, null, null, null, null);
                if (cursor.moveToFirst()) {
                    do {
                        String name = cursor.getString(cursor.getColumnIndex("name"));
                        String author = cursor.getString(cursor.getColumnIndex("author"));
                        int pages = cursor.getInt(cursor.getColumnIndex("pages"));
                        double price = cursor.getDouble(cursor.getColumnIndex("price"));

                        Log.d(TAG, "book name is " + name);
                        Log.d(TAG, "book author is " + author);
                        Log.d(TAG, "book pages is " + pages);
                        Log.d(TAG, "book price is " + price);
                    } while (cursor.moveToNext());
                }
                cursor.close();

                //--------------------- dbflow --------------------------
                DBFlowModel dbFlowModel = new Select().from(DBFlowModel.class).where(DBFlowModel_Table.modelname.is("dbtest1")).querySingle();
                if (dbFlowModel != null) {
                    //Log.d("MainActivity", "phone is " +  dbFlowModel.getPhone());

                    Log.d(TAG, "V=" + VERSION + ", name is " + dbFlowModel.getName());
                    Toast.makeText(context, "V=" + VERSION + ", name is " + dbFlowModel.getName(), Toast.LENGTH_SHORT).show();

                    if (VERSION != 1) {
                        Log.d(TAG, "phone is " + dbFlowModel.getPhone());
                        Toast.makeText(context, "phone is " + dbFlowModel.getPhone(), Toast.LENGTH_SHORT).show();
                        Toast.makeText(context, "email is " + dbFlowModel.getEmail(), Toast.LENGTH_SHORT).show();
                    }
                } else {
                    Log.d(TAG, "dbFlowModel is null");
                    Toast.makeText(context, "dbFlowModel is null", Toast.LENGTH_SHORT).show();
                }
            }
        });

        //修改表结构
        Button changeTable = (Button) findViewById(R.id.change_table);
        changeTable.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                //--------------------- sqlite --------------------------
                SQLiteDatabase db = dbHelper.getWritableDatabase();
                db.execSQL("ALTER TABLE Book ADD COLUMN alias text;");

                //dbflow
                //DBFlowModel dbFlowModel = new Select().from(DBFlowModel.class).where(DBFlowModel_Table.name.is("Ruomiz")).querySingle();
                //Log.d("MainActivity", "phone is " +  dbFlowModel.phone);
            }
        });
    }
}
